package com.herench.bunch.clinic.convert;

import ca.uhn.hl7v2.model.GenericPrimitive;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.datatype.CP;
import ca.uhn.hl7v2.model.v24.group.MFN_M04_MF_CDM;
import ca.uhn.hl7v2.model.v24.message.MFK_M01;
import ca.uhn.hl7v2.model.v24.message.MFN_M04;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.clinic.handler.ProxyMsgCodeConstant;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.support.RequestMessage;
import org.dom4j.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class PickupAppointConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Document doToMessage(RequestMessage requestMessage) throws Exception {
        Document document = ProxyMsgTemplate.getInMsgTpl(ProxyMsgCodeConstant.PICK_UP_APPOINT_CODE);
        MFN_M04 hl7Message = (MFN_M04) requestMessage.getHl7Message();
        MFN_M04_MF_CDM mfCdm = hl7Message.getMF_CDM();
        MFE mfe = mfCdm.getMFE();
        int k = mfe.getMfe4_PrimaryKeyValueMFEReps();
        Varies[] varies = mfe.getMfe4_PrimaryKeyValueMFE();
        for(int j=0;j<k;j++){
            GenericPrimitive primitive = (GenericPrimitive) varies[j].getData();
            String[] temp = primitive.getValue().split("=");
            if (temp.length>1){
                document.getRootElement().addElement(temp[0]).addText(temp[1] != null ? temp[1] : "");
            }else {
                document.getRootElement().addElement(temp[0]).addText("");
            }
        }

        PRC prc = mfCdm.getPRC();
        String SblwskIn = prc.getPrc2_FacilityIDPRC(0).getCe1_Identifier().getValue();
        document.getRootElement().addElement("SblwskIn").addText(SblwskIn != null ? SblwskIn : "");
        String SblwskOut = prc.getPrc2_FacilityIDPRC(1).getCe1_Identifier().getValue();
        document.getRootElement().addElement("SblwskOut").addText(SblwskOut != null ? SblwskOut : "");

        CE department = prc.getPrc3_Department(0);

        String deptCode = department.getCe1_Identifier().getValue();
        document.getRootElement().addElement("DeptCode").addText(deptCode != null ? deptCode : "");

        String doctorCode = department.getCe2_Text().getValue();
        document.getRootElement().addElement("DoctorCode").addText(doctorCode != null ? doctorCode : "");

        CP cp = prc.getPrc5_Price(0);
        document.getRootElement().addElement("RegFee").addText("0");
        document.getRootElement().addElement("InspectFee").addText(cp.getCp1_Price().getMo2_Denomination().getValue());
        document.getRootElement().addElement("TotalFee").addText(cp.getCp1_Price().getMo2_Denomination().getValue());
        document.getRootElement().addElement("APW").addText(prc.getPrc16_ActiveInactiveFlag().getValue());

        String res = document.asXML().replace("&#6;",String.valueOf((char)6));
        logger.info(requestMessage.getMsgCode() + " to HIS XML = "+ res);
        return document;
    }

    @Override
    public Message doFromMessage(String message) throws Exception {
        Assert.notNull(message, "message 为空");
        logger.info("200060 from HIS XML = "+ message);
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message);
        MFK_M01 mfk = new MFK_M01();
        mfk.initQuickstart("MFK_M01", "MFK_M01", "A");
        MSH msh = mfk.getMSH();
        initResponseMessageMSH(msh);
        MSA msa = mfk.getMSA();
        msa.getErrorCondition().getCe1_Identifier().setValue(doc.getRootElement().elementTextTrim("IsSuccessed"));
        msa.getErrorCondition().getCe2_Text().setValue(doc.getRootElement().elementTextTrim("ErrorMsg"));
        MFI mfi = mfk.getMFI();

        mfi.getMfi1_MasterFileIdentifier().getCe1_Identifier().setValue("RegFee="+doc.getRootElement().elementTextTrim("RegFee"));
        mfi.getMfi1_MasterFileIdentifier().getCe2_Text().setValue("InspectFee="+doc.getRootElement().elementTextTrim("InspectFee"));
        mfi.getMfi1_MasterFileIdentifier().getCe3_NameOfCodingSystem().setValue("TotalFee="+doc.getRootElement().elementTextTrim("TotalFee"));
        mfi.getMfi1_MasterFileIdentifier().getCe4_AlternateIdentifier().setValue("InsurePay="+doc.getRootElement().elementTextTrim("InsurePay"));
        mfi.getMfi1_MasterFileIdentifier().getCe5_AlternateText().setValue("AccountPay="+doc.getRootElement().elementTextTrim("AccountPay"));
        mfi.getMfi1_MasterFileIdentifier().getCe6_NameOfAlternateCodingSystem().setValue("CashPay="+doc.getRootElement().elementTextTrim("CashPay"));
        mfi.getMfi3_FileLevelEventCode().setValue("DivisionID="+doc.getRootElement().elementTextTrim("DivisionID"));
        return mfk;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        return null;
    }
}
